package com.tunan.lsj.business.house.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.EasyUiPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tunan.common.Result;
import com.tunan.common.resultSub.ResultFailure;
import com.tunan.common.resultSub.ResultSuccess;
import com.tunan.lsj.business.house.service.GgFwxxfhService;
import com.tunan.persistence.model.GgFwxxfh;
import com.tunan.persistence.model.TSysUser;
import com.tunan.system.service.IdentifierGenerator;
import com.tunan.system.web.BaseController;

@Controller
@RequestMapping(value = "GgFwxxfh")
public class GgFwxxfhController extends BaseController {
	@Autowired
	private GgFwxxfhService ggFwxxfhService;
	
	@Autowired
	private IdentifierGenerator identifierGenerator;
	
	private Logger log = Logger.getLogger(GgFwxxfhController.class);

	@RequestMapping(value = "addGgFwxxfh", method = RequestMethod.GET)
	public ModelAndView addGgFwxxfh(String id, HttpServletRequest request) {
		if (StringUtils.isNotBlank(id)) {
			request.setAttribute("vo", ggFwxxfhService.selectOne(id));
		}
		return new ModelAndView("/lsj/house/addGgFwxxfh");
	}

	@RequestMapping(value = "ggFwxxfhList", method = RequestMethod.GET)
	public ModelAndView sysMenuList() {
		return new ModelAndView("/lsj/house/ggFwxxfhList");
	}

	/**
	 * 查询房屋套
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "queryAll", method = RequestMethod.POST)
	@ResponseBody
	public EasyUiPage list(@ModelAttribute("pageParams") Page pageParams,
			TSysUser params) {
		PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
		Page pageresult = (Page) ggFwxxfhService.selectAll(null);
		return pageresult.getEasyUiPage();
	}

	
	/**
	 * 新增和修改
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Result save(@ModelAttribute("ggFwxxfh") GgFwxxfh ggFwxxfh) {
		ResultSuccess rs = new ResultSuccess();
		ResultFailure rf = new ResultFailure();
		try {
			if (ggFwxxfh != null) {
				ggFwxxfh.setJwh("五山");
				ggFwxxfh.setDy("呵呵");
				ggFwxxfh.setXzq("不知道");
				ggFwxxfh.setJz("4356");
				ggFwxxfh.setPcs("五山派出所");
				if (StringUtils.isNotBlank(ggFwxxfh.getFhid())) {
					ggFwxxfhService.update(ggFwxxfh);
					rs.setMessage("修改成功");
				} else {
					ggFwxxfh.setFhid(identifierGenerator.generate());
					ggFwxxfhService.insertSelective(ggFwxxfh);
					rs.setMessage("新增成功");
				}

			}
			return rs;

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		rf.setMessage("操作失败");
		return rf;
	}
	
	
	/**
	 * 删除
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Result delete(String id) {
		ResultSuccess rs = new ResultSuccess();
		ResultFailure rf = new ResultFailure();
		try {
			if (StringUtils.isNotBlank(id)) {
				ggFwxxfhService.delete(id);
			}
			rs.setMessage("删除成功");
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		rf.setMessage("删除失败");
		return rf;
	}

	/**
	 * 批量删除
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "batchDel", method = RequestMethod.POST)
	public Result batchDel(String ids) {
		ResultSuccess rs = new ResultSuccess();
		ResultFailure rf = new ResultFailure();
		try {
			if (StringUtils.isNotBlank(ids)) {
				List<String> idList = new ArrayList<String>();
				if (ids.contains(",")) {
					idList = Arrays.asList(ids.split(","));
				} else {
					idList.add(ids);
				}
				ggFwxxfhService.batchDel(idList);
			}
			rs.setMessage("批量删除成功");
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		rf.setMessage("批量删除失败");
		return rf;
	}

}
